package com.ifacebox.web.admin.master.dao;

import com.ifacebox.web.admin.master.model.MasterQuartz;
import com.ifacebox.web.admin.master.model.MasterQuartzQuery;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author znn
 */
@Mapper
@Component
public interface MasterQuartzDao {

    @Select({"<script>"
            , "SELECT * FROM master_quartz"
            , "<where>"
            , "<if test='createUser != null and !createUser.isEmpty()'>create_user = #{createUser}</if>"
            , "<if test='jobStatus != null and !jobStatus.isEmpty()'>job_status = #{jobStatus}</if>"
            , "</where>"
            , "ORDER BY create_time DESC LIMIT #{offset}, #{limit}"
            , "</script>"})
    List<MasterQuartz> query(MasterQuartzQuery masterQuartzQuery);

    @Select({"<script>"
            , "SELECT COUNT(*) FROM master_quartz"
            , "<where>"
            , "<if test='createUser != null and !createUser.isEmpty()'>create_user = #{createUser}</if>"
            , "<if test='jobStatus != null and !jobStatus.isEmpty()'>job_status = #{jobStatus}</if>"
            , "</where>"
            , "</script>"})
    Long total(MasterQuartzQuery masterQuartzQuery);

    @Select({"<script>"
            , "SELECT * FROM master_quartz WHERE id = #{id}"
            , "</script>"})
    MasterQuartz queryById(Integer id);

    @Insert({"<script>"
            , "INSERT INTO master_quartz"
            , "( job_key, job_name, job_explain, class_name, cron_expression, create_user )"
            , "VALUES ( #{jobKey}, #{jobName}, #{jobExplain}, #{className}, #{cronExpression}, #{createUser} )"
            , "</script>"})
    @Options(useGeneratedKeys = true, keyProperty = "id")
    Integer save(MasterQuartz masterQuartz);

    @Update({"<script>"
            , "UPDATE master_quartz SET"
            , "job_name = #{jobName}, job_explain = #{jobExplain}, class_name = #{className}"
            , ", cron_expression = #{cronExpression}, update_user = #{updateUser}, update_time = now()"
            , " WHERE id = #{id}"
            , "</script>"})
    Integer edit(MasterQuartz masterQuartz);

    @Update({"<script>"
            , "UPDATE master_quartz SET job_status = #{jobStatus}, update_time = now() WHERE job_key = #{jobKey}"
            , "</script>"})
    Integer editJobStatusByJobKey(String jobStatus, String jobKey);

    @Delete({"<script>"
            , "DELETE FROM master_quartz WHERE id = #{id}"
            , "</script>"})
    Integer delete(Integer id);
}
